Skip to content

fix: strip call_id and response_item_id from tool_calls for Mistral compatibility#864

Closed
unmodeled-tyler wants to merge 4 commits intoNousResearch:mainfrom
unmodeled-tyler:fix/mistral-tool-calls-clean
Closed

fix: strip call_id and response_item_id from tool_calls for Mistral compatibility#864
unmodeled-tyler wants to merge 4 commits intoNousResearch:mainfrom
unmodeled-tyler:fix/mistral-tool-calls-clean

Conversation

@unmodeled-tyler
Copy link
Copy Markdown
Contributor

Summary

  • Strips call_id and response_item_id from tool_calls before sending to strict APIs like Mistral
  • Fixes 429 rate limit handling (was incorrectly treated as non-retryable)
  • Updates .gitignore with common patterns

Problem

Mistral's API strictly validates the Chat Completions schema and rejects unknown fields with 422 - Extra inputs are not permitted. The call_id and response_item_id fields were added for Codex Responses API support (ce175d7) but are not part of the Chat Completions spec.

This extends the fix from PR #253 (which stripped finish_reason) to also strip these internal fields.

Changes

  1. Strip call_id and response_item_id from tool_calls in 3 locations:

    • Main conversation loop
    • _handle_max_iterations()
    • flush_memories()
  2. Fix 429 rate limit handling - Changed from non-retryable to retryable with exponential backoff

  3. Update .gitignore - Added config.yaml, sessions/, *.log, .DS_Store, Thumbs.db

Safety

The Codex Responses API code path has built-in fallback logic that uses the id field when call_id is not present, so this change is safe for both API modes:

  • Chat Completions API (Mistral, OpenRouter, etc.): Correctly removes non-spec fields
  • Codex Responses API: Safe - has fallback to id field

Related

Testing

Tested with Mistral API via Telegram gateway - tool calls now work correctly without 422 errors.

…ompatibility

Mistral's API strictly validates the Chat Completions schema and rejects
unknown fields with 422 "Extra inputs are not permitted". The call_id and
response_item_id fields were added for Codex Responses API support (ce175d7)
but are not part of the Chat Completions spec.

This extends the fix from PR NousResearch#253 (which stripped finish_reason) to also
strip these internal fields before sending to strict APIs like Mistral.

Changes:
- Strip call_id and response_item_id from tool_calls in 3 locations:
  - Main conversation loop
  - _handle_max_iterations()
  - flush_memories()
- Fix 429 rate limit handling (was incorrectly treated as non-retryable)
- Update .gitignore with common patterns

The Codex Responses API code path has built-in fallback logic that uses
the 'id' field when call_id is not present, so this change is safe for
both API modes.
teknium1 added a commit that referenced this pull request Mar 12, 2026
…tibility

Mistral's API strictly validates the Chat Completions schema and rejects
unknown fields (call_id, response_item_id) with 422. These fields are
added by _build_assistant_message() for Codex Responses API support.

This fix:
- Only strips when targeting Mistral (api.mistral.ai in base_url)
- Creates new tool_call dicts instead of mutating originals (shallow
  copy safety — msg.copy() shares the tool_calls list)
- Preserves call_id/response_item_id in the internal message history
  so _chat_messages_to_responses_input() can still read them if the
  session falls back to a Codex provider mid-conversation

Applied in all 3 API message building locations:
- Main conversation loop (run_conversation)
- _handle_max_iterations()
- flush_memories()

Inspired by PR #864 (unmodeled-tyler) which identified the issue but
applied the fix unconditionally and mutated originals via shallow copy.

Co-authored-by: unmodeled-tyler <unmodeled.tyler@proton.me>
@teknium1
Copy link
Copy Markdown
Contributor

Thanks for identifying this issue, @unmodeled-tyler! The Mistral 422 fix has been merged via PR #1058 with a more targeted approach:

  • Only strips call_id/response_item_id when targeting Mistral (api.mistral.ai), not all providers
  • Creates new tool_call dicts instead of mutating via shallow copy, preserving the fields in internal message history for Codex Responses API compatibility
  • Avoids the autoformatter noise and provider router regression from the stale branch merge

Your contribution is credited via Co-authored-by in the commit. Thanks for the report and the fix idea!

@teknium1 teknium1 closed this Mar 12, 2026
@unmodeled-tyler unmodeled-tyler deleted the fix/mistral-tool-calls-clean branch March 14, 2026 05:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants